$(document)
		.ready(
				function() {
					var clientHeight = top.document.body.clientHeight - 200;
					var clientWidth = document.body.clientWidth - 100;
					$("button").button();
					var varname = $("#varname"), varcode = $("#varcode"), parentid = $("#parentid"), address = $("#address"), owner = $("#owner"), conname = $("#conname"), conphone = $("#conphone"), province = $("#province"), city = $("#city"), validdate = $("#validdate"), remark = $("#remark"), allFields = $(
							[]).add(varname).add(varcode).add(parentid).add(
							address).add(owner).add(conname).add(conphone).add(
							province).add(city).add(validdate).add(remark);
					allFields.attr("value", "");
					$("#add").click(function() {
						$("#addinfo").dialog("open");
					});
					$("#mod").click(function() {
					});
					$("#del").click(function() {
					});
					$("#validdate").datepicker($.datepicker.regional["zh-CN"]);
					$("#addinfo")
							.dialog(
									{
										autoOpen : false,
										height : clientHeight,
										width : clientWidth,
										modal : true,
										buttons : {
											"Submit" : function() {
												var bValid = true;
												allFields
														.removeClass("ui-state-error");
												bValid = bValid
														&& checkLength(varname,
																"单位名称", 1, 16);
												bValid = bValid
														&& checkLength(varcode,
																"单位编号", 6, 16);
												bValid = bValid
														&& checkLength(
																parentid,
																"上级单位", 1, 160);
												var jsonvalue = {
													varname : varname
															.attr("value"),
													varcode : varcode
															.attr("value"),
													parentid : parentid.find(
															"option:selected")
															.attr("value"),
													address : address
															.attr("value"),
													owner : owner.attr("value"),
													conname : conname
															.attr("value"),
													conphone : conphone
															.attr("value"),
													province : province
															.attr("value"),
													city : city.attr("value"),
													validdate : validdate
															.attr("value"),
													remark : remark
															.attr("value")
												};
												if (bValid) {
													jQuery
															.getJSON(
																	"SYS/unitInfoAction!addUnitInfos.action",
																	jsonvalue,
																	function Callback(
																			data) {
																		addBackInfo(data);
																	}, "json");
												}
											},
											Cancel : function() {
												$(this).dialog("close");
											}
										}
									});
					$("#parentid")
							.click(function() {
								// $("#parentid option").remove();
									if ($("#parentid option").length == 2)
										jQuery
												.getJSON(
														"SYS/unitInfoAction!parentUnitInfos.action",
														function Callback(data) {
															parentBackInfo(
																	this, data);
														}, "json");
								});
				});
function addBackInfo(data) {
	data = eval("(" + data + ")");
	if (data.errnum == 0) {
		if (confirm("添加成功")) {
			$("#addinfo").dialog("close");
		}
	} else {
		alert(data.errmsg);
	}
}
function parentBackInfo(coms, data) {
	data = eval(data);
	if (data != null)
		$.each(data, function(i, info) {
			$("#parentid").append(
					"<option value=" + info.id + ">" + info.varname
							+ "</option>");
		});
}